var infoDlg ;
var pwdDlg;

$(document).ready(
	function(){ 
		$('#infoDlg')[0].style.display = "none";
		$('#pwdDlg')[0].style.display = "none";
		// 配置对话框
		infoDlg = $("#infoDlg");
		pwdDlg = $("#pwdDlg");
		$("#infoDlg").dialog( {
			autoOpen : false,
			modal : true, // 设置对话框为模态（modal）对话框
			resizable : true,
			width : 480,
			buttons : { // 为对话框添加按钮
				"关闭" : function() {
					$("#infoDlg").dialog("close")
				},
				"保存" : updateUserInfoSimple,
				"修改密码" : openUpdatePwd
			}
		});  
		$("#pwdDlg").dialog( {
			autoOpen : false,
			modal : true, // 设置对话框为模态（modal）对话框
			resizable : true,
			width : 250,
			buttons : { // 为对话框添加按钮
				"取消" : function() {
					$("#pwdDlg").dialog("close")
				},
				"确认修改" : updateUserPwd
			}
		});  
	}
);	

var showModiInfo = function() {
	var dialogButtonPanel = infoDlg.siblings(".ui-dialog-buttonpane");  
	infoDlg.find("input").removeAttr("disabled").val("");  
	//dialogButtonPanel.find("button:not(:contains('取消'))").hide();  
	//dialogButtonPanel.find("button:contains('添加')").show();  
	infoDlg.find("#eID").attr("disabled", true);
	infoDlg.dialog("option", "title", "修改个人信息 ");  
	
	loadSelectedRowDataS();
}

var loadSelectedRowDataS = function() {
		// 从Server读取当前用户的JSON数据
		$.ajax( {
			type:"POST",   
			url : getContextPath()+"/user/curUser.do",
			data : {},
			dataType : "json",
			//contentType: "application/json",
			cache : false,
			async:false,
			error : function(textStatus, errorThrown) {
				alert("系统ajax交互错误: " + textStatus.responseText);
			},
			success : function(data, textStatus) {
				// 如果读取结果成功，则将信息载入到对话框中     
				var rowData = data;
				//var infoDlg = $("#infoDlg");

				infoDlg.find('#eID').val(rowData.id);
				infoDlg.find("#eName").val(rowData.name);
				infoDlg.find("#eMobilePhone").val(rowData.mobilePhone);
				infoDlg.find("#eMailAddress").val(rowData.mailAddress);
				infoDlg.find("#longitude").val(rowData.longitude);
				infoDlg.find("#latitude").val(rowData.latitude);
				infoDlg.find("#mapZoom").val(rowData.mapZoom);								

				// 打开对话框
				infoDlg.dialog("open");
			}
		});
};

var checkUserModifyValid = function() {
	var eName = $.trim(infoDlg.find("#eName").val());
	if (eName=="") {
		alert("'用户姓名'为必填信息");
		return false;
	}
};

var updateUserInfoSimple = function() {
	if (checkUserModifyValid()==false) return false;
	
	var eID = $.trim(infoDlg.find("#eID").val());
	var eName = $.trim(infoDlg.find("#eName").val());
	var eMobilePhone = $.trim(infoDlg.find("#eMobilePhone").val());
	var eMailAddress = $.trim(infoDlg.find("#eMailAddress").val());
	var elongitude = $.trim(infoDlg.find("#longitude").val());
	var elatitude = $.trim(infoDlg.find("#latitude").val());
	var eMapZoom = $.trim(infoDlg.find("#mapZoom").val());
	
	var params = {
			"eID" : eID,
			"eName" : eName,
			"eMobilePhone" : eMobilePhone,
			"eMailAddress" : eMailAddress,
			"elongitude" : elongitude,
			"elatitude" : elatitude,
			"eMapZoom" : eMapZoom
	};
	
	var actionUrl = getContextPath()+"/authority/users/updateUserSimple.do";
	
	$.ajax( {
		type : "POST",
		url : actionUrl,
		data : params,
		dataType : "json",
		cache : false,
		error : function(textStatus, errorThrown) {
			alert("系统ajax交互错误: " + textStatus);
		},
		success : function(data, textStatus) {
			if (data != null) {
				alert("用户信息更新成功!");
			} else {
				alert("用户帐号重复，修改操作失败!");
			}
		}
	});
};

var openUpdatePwd = function() {
	//打开修改密码的对话框
	var dialogButtonPanel = pwdDlg.siblings(".ui-dialog-buttonpane");  
	pwdDlg.find("input").removeAttr("disabled").val("");  
	pwdDlg.dialog("option", "title", "修改登录密码 ").dialog("open");  	
};

var checkPwdSame = function() {
	var newPwd1 = $.trim(pwdDlg.find("#newPwd1").val());
	var newPwd2 = $.trim(pwdDlg.find("#newPwd2").val());
	if (newPwd1=="") {
		alert("请输入新密码");
		return false;
	}
	if (newPwd2=="") {
		alert("请确认新密码");
		return false;
	}
	if (newPwd1!=newPwd2) {
		alert("新密码不一致");
		return false;
	}
};

var updateUserPwd = function() {
	if (checkPwdSame()==false) return false;
	
	var oriPwd = $.trim(pwdDlg.find("#oriPwd").val());
	var newPwd1 = $.trim(pwdDlg.find("#newPwd1").val());
	var newPwd2 = $.trim(pwdDlg.find("#newPwd2").val());
	var params = {
			"oriPwd" : oriPwd,
			"newPwd1" : newPwd1,
			"newPwd2" : newPwd2
	};
	
	var actionUrl = getContextPath()+"/authority/users/updateUserPwd.do";
	
	$.ajax( {
		type : "POST",
		url : actionUrl,
		data : params,
		cache : false,
		error : function(textStatus, errorThrown) {
			alert("系统ajax交互错误: " + textStatus);
		},
		success : function(data, textStatus) {
			if (data != null) {
				if (data=="oriError") {
					alert("原密码不正确!");
				} 
				if (data=="pwdChanged") {
					pwdDlg.dialog("close");
					alert("密码修改成功!");	
				}
				if (data=="failed") {
					alert("修改密码失败!");
				}
			} else {
				alert("修改密码失败!");
			}
		}
	});
};